uniform mat4 uMVPMatrix;    //the final matrix : ModeView matrix * project matrix * current matrix
uniform mat4 uMMatrix;      //transform matrix : ModeView matrix * project matrix 
uniform vec3 uLightLocation;//light position
uniform vec3 uCamera;       //camera position
attribute vec3 aPosition;   //vertex position
attribute vec3 aNormal;     //normal of the vertex
attribute vec2 aTexCoor;    //texture of the vertex

//varying values to fragment shader
varying vec4 ambient;
varying vec4 diffuse;
varying vec4 specular;
varying vec2 vTextureCoord; 

//calculate position lighting method
void calLight(
    in vec3 normal,                 //transform in value
    in vec3 lightLocation,          //light Location
    in vec4 lightAmbient,           //light Ambient
    in vec4 lightDiffuse,           //light Diffuse
    in vec4 lightSpecular,          //light Specular
    inout vec4 ambient,             //transform out value ambient light 
    inout vec4 diffuse,             //transform out value diffuse light
    inout vec4 specular             //transform out value specular light
){
    ambient = lightAmbient;                         //direct give the value
    vec3 normalTarget = aPosition + normal;         //calculate normal after the transform
    vec3 newNormal = (uMMatrix * vec4(normalTarget,1)).xyz - (uMMatrix * vec4(aPosition,1)).xyz;
    newNormal = normalize(newNormal);               //normalize the normal
    //calculate a vector form surface point to camera location
    vec3 eye = normalize(uCamera -(uMMatrix * vec4(aPosition,1)).xyz);
    //calculate a vector form surface point to light location
    vec3 vp = normalize(lightLocation-(uMMatrix*vec4(aPosition,1)).xyz);
    vec3 halfVector = normalize(vp + eye);          //求视线与光线的半向量
    float shininess = 50.0;//粗糙度，越小越光滑
    float nDotViewPosition = max(0.0,dot(newNormal,vp)); //求法向量与vp的点积与0的最大值
    diffuse = lightDiffuse * nDotViewPosition;           //计算散射光的最终强度
    float nDotViewHalfVector = dot(newNormal,halfVector);//法线与半向量的点积 
    float powerFactor = max(0.0,pow(nDotViewHalfVector,shininess)); //镜面反射光强度因子
    specular=lightSpecular * powerFactor;                //计算镜面光的最终强度
}

void main()
{
    gl_Position = uMVPMatrix * vec4(aPosition,1);
    vec4 ambientTemp, diffuseTemp, specularTemp;
    calLight
    (
        normalize(aNormal),
        vec4(0.15,0.15,0.15,1.0),
        vec4(0.9,0.9,0.9,1.0),
        vec4(0.4,0.4,0.4,1.0),
        ambientTemp,
        diffuseTemp,
        specularTemp,
        uLightLocation
    );

   ambient = ambientTemp;
   diffuse = diffuseTemp;
   specular = specularTemp;
   vTextureCoord = aTexCoor;
}